package me.lc.linklinkchrist.sprite;

import me.lc.linklinkchrist.R;
import me.lc.linklinkchrist.core.GameManager;
import me.lc.linklinkchrist.core.GameSystem;
import me.lc.linklinkchrist.core.GameType;
import me.lc.linklinkchrist.core.StageManager;
import me.lc.linklinkchrist.entity.StageInfo;
import me.lc.linklinkchrist.scene.GameScene;
import me.lc.linklinkchrist.scene.MainScene;
import me.lc.linklinkchrist.util.ADHelper;
import me.lc.linklinkchrist.util.AudioHelper;
import me.lc.linklinkchrist.util.GameUtil;
import android.app.Activity;

import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.IntervalAction;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.actions.ScaleTo;
import com.wiyun.engine.actions.Sequence;
import com.wiyun.engine.actions.ease.EaseBackInOut;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.game.WiGame;

/**
 * 胜利画面Block
 * 
 * @author Administrator
 * 
 */
public class WinShowBlock extends Node {

	Sprite bg;
	Sprite star1;
	Sprite star2;
	Sprite star3;
	Button btnNext;
	Button btnRetry;
	Button btnMainmenu;
	Label gameScore;
	Label comboScore;
	Label restTime;
	Label totalScore;
	Label addGold;

	static int normalFontSize = 20;
	static int addGoldFontSize = 15;

	public WinShowBlock() {
		bg = Sprite.make(GameSystem.winBgTex);
		addChild(bg);

		float labelX = GameSystem.winBgTex.getWidth() * 0.589f
				- GameSystem.winBgTex.getWidth() / 2.0f;
		float labelY1 = GameSystem.winBgTex.getHeight() * 0.758f
				- GameSystem.winBgTex.getHeight() / 2.0f;
		gameScore = Label.make(0 + "", normalFontSize);
		gameScore.setAnchorPercent(0, 0);
		gameScore.setColor(WYColor3B.make(255, 255, 255));
		gameScore.setPosition(labelX, labelY1);
		addChild(gameScore);

		float labelY2 = GameSystem.winBgTex.getHeight() * 0.63f
				- GameSystem.winBgTex.getHeight() / 2.0f;
		comboScore = Label.make(0 + "", normalFontSize);
		comboScore.setAnchorPercent(0, 0);
		comboScore.setColor(WYColor3B.make(255, 255, 255));
		comboScore.setPosition(labelX, labelY2);
		addChild(comboScore);

		float labelY3 = GameSystem.winBgTex.getHeight() * 0.51f
				- GameSystem.winBgTex.getHeight() / 2.0f;
		restTime = Label.make(0 + "", normalFontSize);
		restTime.setAnchorPercent(0, 0);
		restTime.setColor(WYColor3B.make(255, 255, 255));
		restTime.setPosition(labelX, labelY3);
		addChild(restTime);

		float labelY4 = GameSystem.winBgTex.getHeight() * 0.39f
				- GameSystem.winBgTex.getHeight() / 2.0f;
		totalScore = Label.make(0 + "", normalFontSize);
		totalScore.setColor(WYColor3B.make(180, 0, 0));
		totalScore.setAnchorPercent(0, 0);
		totalScore.setPosition(labelX, labelY4);
		addChild(totalScore);

		float labelY5 = GameSystem.winBgTex.getHeight() * 0.2996f
				- GameSystem.winBgTex.getHeight() / 2.0f;
		addGold = Label.make(0 + "", addGoldFontSize);
		addGold.setColor(WYColor3B.make(255, 255, 255));
		addGold.setAnchorPercent(0, 0);
		addGold.setPosition(labelX, labelY5);
		addChild(addGold);

		// 设置按钮位置
		btnNext = Button.make(R.drawable.button_next, R.drawable.button_next,
				this, "nextBtnClicked");
		float nextY = GameSystem.winBgTex.getHeight() * 0.215f
				- GameSystem.winBgTex.getHeight() / 2.0f;
		btnNext.setPosition(0, nextY);
		btnNext.setClickScale(1.05f);
		addChild(btnNext);

		btnRetry = Button.make(R.drawable.button_retry_win,
				R.drawable.button_retry_win, this, "retryBtnClicked");
		btnRetry.setAnchorPercent(0, 0);
		float retryX = GameSystem.winBgTex.getWidth() * 0.081f
				- GameSystem.winBgTex.getWidth() / 2.0f;
		float retryY = GameSystem.winBgTex.getHeight() * 0.085f
				- GameSystem.winBgTex.getHeight() / 2.0f;
		btnRetry.setPosition(retryX, retryY);
		btnRetry.setClickScale(1.05f);
		addChild(btnRetry);

		btnMainmenu = Button.make(R.drawable.button_returnmenu_win,
				R.drawable.button_returnmenu_win, this, "menuBtnClicked");
		btnMainmenu.setAnchorPercent(0, 0);
		float menuX = GameSystem.winBgTex.getWidth() * 0.547f
				- GameSystem.winBgTex.getWidth() / 2.0f;
		btnMainmenu.setPosition(menuX, retryY);
		btnMainmenu.setClickScale(1.05f);
		addChild(btnMainmenu);

		// star
		float starY = GameSystem.winBgTex.getHeight() * 0.889f
				- GameSystem.winBgTex.getHeight() / 2.0f;
		star1 = Sprite.make(GameSystem.star1Tex);
		star1.setPosition(0, starY);
		star1.setVisible(false);
		addChild(star1);

		star2 = Sprite.make(GameSystem.star2Tex);
		star2.setPosition(0, starY);
		star2.setVisible(false);
		addChild(star2);

		star3 = Sprite.make(GameSystem.star3Tex);
		star3.setPosition(0, starY);
		star3.setVisible(false);
		addChild(star3);
	}

	public void showScore() {
		btnNext.setVisible(true);
		star1.setVisible(false);
		star2.setVisible(false);
		star3.setVisible(false);
		// unlock next stage
		// 判断不是最后一关
		if (GameManager.instance.CurrentGameType == GameType.Time) {
			// 如果是争分夺秒模式，隐藏下一关按钮
			btnNext.setVisible(false);
		} else {
			// 写死
			if (StageManager.instance.currentStage + 1 < 100) {
				btnNext.setVisible(true);
				StageInfo nextStage = StageManager.instance.stageInfos
						.get(StageManager.instance.currentStage + 1);
				nextStage.setLock(false);
				// 设定下一个关卡的分数信息
				StageManager.instance.stageScores.put(nextStage.getStageName(),
						0);
			} else {
				btnNext.setVisible(false);
				// 完成所有关卡
				WiGame.unlockAchievement("e556d456bd4a1886");
			}
		}

		int game = GameManager.instance.getStageScore();
		int combo = GameManager.instance.getLongCombo() * 100;
		int time = 0;
		if (!GameManager.instance.isTimeAdded()) {
			time = GameManager.instance.getCurrentGameSecond() * 100;
		}
		int total = game + combo + time;
		int gold = total / 88;
		gameScore.setText(game + "");
		comboScore.setText(combo + "");
		restTime.setText(time + "");
		totalScore.setText(total + "");
		addGold.setText(gold + "");

		StageManager.instance.goldCount += gold;

		if (GameManager.instance.CurrentGameType == GameType.Normal) {
			// 保存本轮分数
			StageInfo curr = StageManager.instance.stageInfos
					.get(StageManager.instance.currentStage);

			curr.setScore(total);
			StageManager.instance.stageScores.put(curr.getStageName(), total);

			// 保存数据
			Thread saveThread = new Thread() {
				@Override
				public void run() {
					// 保存关卡分数
					StageManager.instance.saveStageScores();
					StageManager.instance.saveGold();
				}
			};
			saveThread.start();
			// 显示star
			showStar(total);
		}

		// 提交总分数微云
		if (GameManager.instance.CurrentGameType == GameType.Normal) {
			int sum = 0;
			for (StageInfo stageInfo : StageManager.instance.stageInfos) {
				if (!stageInfo.isLock() && stageInfo.getScore() > 0) {
					sum += stageInfo.getScore();
				}
			}
			StageManager.instance.totalCouples += (GameSystem.GameColumns * GameSystem.GameRows) / 2;
			StageManager.instance.saveTotalCouples();
			// 提交分数
			// 关卡总分
			WiGame.submitScore("81d7c1d51089325f", sum, null, true);
		} else if (GameManager.instance.CurrentGameType == GameType.Time) {
			StageManager.instance.totalCouples += GameManager.instance
					.getTimeGameClearCount();
			StageManager.instance.saveTotalCouples();

			// 提交分数
			// 争分夺秒分数
			WiGame.submitScore("a55c908f72a7e254", total, null, true);
		}

		if (GameManager.instance.CurrentGameType == GameType.Normal
				&& StageManager.instance.currentStage == 0) {
			// 完成快乐圣诞节
			WiGame.unlockAchievement("f96c16756c108185");
		}
	}

	/**
	 * 显示关卡星星
	 * 
	 * @param total
	 */
	private void showStar(int total) {
		Sprite star = star1;
		if (total >= GameSystem.Star3Score) {
			star = star3;
		} else if (total >= GameSystem.Star2Score) {
			star = star2;
		}
		star.setVisible(true);
		IntervalAction bigger = (IntervalAction) ScaleTo.make(0.5f, 0.8f, 1.1f)
				.autoRelease();
		IntervalAction smaller = (IntervalAction) bigger.reverse()
				.autoRelease();
		IntervalAction seq = (IntervalAction) Sequence.make(
				EaseBackInOut.make(bigger), EaseBackInOut.make(smaller))
				.autoRelease();
		Action repeat = (Action) RepeatForever.make(seq).autoRelease();
		star.runAction(repeat);
	}

	public void nextBtnClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.clickItem();
				// 消除总数
				WiGame.submitScore("8e1136e1d33ba9e6",
						StageManager.instance.totalCouples, null, true);
				StageManager.instance.currentStage = StageManager.instance.currentStage + 1;
				GameScene.make().startNewGame();
				WinShowBlock.this.setScale(0.5f);
			}
		});
	}

	public void retryBtnClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.clickItem();
				// 消除总数
				WiGame.submitScore("8e1136e1d33ba9e6",
						StageManager.instance.totalCouples, null, true);
				if (GameManager.instance.CurrentGameType == GameType.Normal) {
					GameScene.make().startNewGame();
				} else {
					GameScene.make().startNewTimeGame();
				}
				WinShowBlock.this.setScale(0.5f);
			}
		});
	}

	public void menuBtnClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.clickItem();
				// 消除总数
				WiGame.submitScore("8e1136e1d33ba9e6",
						StageManager.instance.totalCouples, null, true);
				MainScene.make().showMainMenu();
				GameManager.instance.quitGame();
				GameUtil.switchSceneFlipX(MainScene.make());
				ADHelper.hideAd();
				WinShowBlock.this.setScale(0.5f);
			}
		});
	}
}
